// 获取全局应用程序实例对象
const app = getApp();
const {get,
  post
} = app.utils.request;
const {
  merchant_list
} = app.utils.config;

Array.prototype.unique = function(key) {
  var arr = this;
  var n = [arr[0]];
  for (var i = 1; i < arr.length; i++) {
    if (key === undefined) {
      if (n.indexOf(arr[i]) == -1) n.push(arr[i]);
    } else {
      inner: {
        var has = false;
        for (var j = 0; j < n.length; j++) {
          if (arr[i][key] == n[j][key]) {
            has = true;
            break inner;
          }
        }
      }
      if (!has) {
        n.push(arr[i]);
      }
    }
  }
  return n;
}

// 组件基础代码
Component({
  behaviors: [],
  properties: {
    list: {
      type: [Array],
      value: [{
        alphabet: 'hot',
        datas: {
          gps: '北京市',
          lateQueryArr: [],
          hotCityArr: []
        },
        active: false
      }, {
        alphabet: 'A',
        datas: [],
        active: false
      }, {
        alphabet: 'B',
        datas: [],
        active: false
      }, {
        alphabet: 'C',
        datas: [],
        active: false
      }, {
        alphabet: 'D',
        datas: [],
        active: false
      }, {
        alphabet: 'E',
        datas: [],
        active: false
      }, {
        alphabet: 'F',
        datas: [],
        active: false
      }, {
        alphabet: 'G',
        datas: [],
        active: false
      }, {
        alphabet: 'H',
        datas: [],
        active: false
      }, {
        alphabet: 'I',
        datas: [],
        active: false
      }, {
        alphabet: 'J',
        datas: [],
        active: false
      }, {
        alphabet: 'K',
        datas: [],
        active: false
      }, {
        alphabet: 'L',
        datas: [],
        active: false
      }, {
        alphabet: 'M',
        datas: [],
        active: false
      }, {
        alphabet: 'N',
        datas: [],
        active: false
      }, {
        alphabet: 'O',
        datas: [],
        active: false
      }, {
        alphabet: 'P',
        datas: [],
        active: false
      }, {
        alphabet: 'Q',
        datas: [],
        active: false
      }, {
        alphabet: 'R',
        datas: [],
        active: false
      }, {
        alphabet: 'S',
        datas: [],
        active: false
      }, {
        alphabet: 'T',
        datas: [],
        active: false
      }, {
        alphabet: 'U',
        datas: [],
        active: false
      }, {
        alphabet: 'V',
        datas: [],
        active: false
      }, {
        alphabet: 'W',
        datas: [],
        active: false
      }, {
        alphabet: 'X',
        datas: [],
        active: false
      }, {
        alphabet: 'Y',
        datas: [],
        active: false
      }, {
        alphabet: 'Z',
        datas: [],
        active: false
      }]
    },
    url: {
      type: [String],
      value: ''
    },
    fid: {
      type: [String,Number],
      value: 34
    }
  },
  data: {
    alpha: 'hot',
    windowHeight: '',
    moveSignIndex: null,
    apHeight: 16,
    offsetTop: 90,
    lateQueryArr: [],
    id: '',
    hidden: false
  },
  methods: {
    processData(data) {
      var data = data;
      var that = this;
      var list = [];

      for (var key in data.list) {
        var curTempDataArr = data.list[key];
        var curkey = key;

        for (var i = 0; i < curTempDataArr.length; i++) {
          var tempObj = {
            alphabet: '',
            datas: [],
            active: false
          };
          tempObj['alphabet'] = curkey;
          tempObj['datas'].push({
            id: curTempDataArr[i].id,
            title: curTempDataArr[i].name
          })

        }
        list.push(tempObj)

      }

      list.unshift({
        alphabet: 'hot',
        datas: {
          gps: '北京市',
          lateQueryArr: [],
          hotCityArr: []
        },
        active: false
      })


      var hotList = [];
      for (var i = 0; i < data.hot.length; i++) {
        var tempHotObj = data.hot[i];
        var tempHotCity = {
          title: tempHotObj['name'],
          cur: false,
          id: tempHotObj['id']
        }
        hotList.push(tempHotCity);

      }

      list[0].datas.hotCityArr = hotList;

      /////城市要去取
      list[0].datas.gps = wx.getStorageSync('city_name');
      list[0].datas.lateQueryArr = that.data.lateQueryArr;

      return list;
    },
    detailTap(e) {
      var that = this;
      let {
        id,
        name
      } = e.target.dataset;

      wx.setStorageSync('city_name', name);
      wx.setStorageSync('city_id', id);

      var lateQuery = {
        title: name,
        cur: false,
        id: id
      };

      that.data.lateQueryArr.push(lateQuery);

      var uniqueLateQueryArr = that.data.lateQueryArr.unique('id');

      wx.setStorageSync('lateQueryArr', uniqueLateQueryArr);
      this.triggerEvent('wiiCitySwitchEvent');

    },
    handlerAlphaTap(e) {
      let {
        ap
      } = e.target.dataset;
      let {
        list
      } = this.data;
      let signIndex = 0;
      var that = this;
      for (var i = 0; i < list.length; i++) {
        if (list[i]['alphabet'] == ap) {
          list[i]['active'] = true;
          signIndex = i;
        } else {
          list[i]['active'] = false;
        }
      }
      this.setData({
        alpha: ap,
        list: list
      });

      var timer = setTimeout(function() {
        list[signIndex]['active'] = false;
        that.setData({
          list: list
        });
        clearTimeout(timer);
      }, 600)
    },
    handlerMove(e) {
      let {
        list
      } = this.data;
      let moveY = e.touches[0].clientY;

      let rY = moveY - this.data.offsetTop;
      if (rY >= 0) {
        let index = Math.ceil((rY - this.data.apHeight) / this.data.apHeight);
        if (0 <= index < list.length) {
          let nonwAp = list[index];
          if (nonwAp) {
            for (var i = 0; i < list.length; i++) {
              if (list[i]['alphabet'] == nonwAp.alphabet) {
                list[i]['active'] = true;
                this.data.moveSignIndex = i;
              } else {
                list[i]['active'] = false;
              }
            }
            this.setData({
              alpha: nonwAp.alphabet,
              list: list
            });
          }
        }
      }
    },
    handlerEnd(e) {
      var that = this;
      let {
        list,
        moveSignIndex
      } = that.data;

      var endTimer = setTimeout(function() {
        (moveSignIndex != null) && (list[moveSignIndex]['active'] = false);
        that.setData({
          list: list
        });
        clearTimeout(endTimer);
      }, 600)
    }
  },
  ready: function () {
    var windowHeight = '667px';
    wx.getSystemInfo({
      success: function(res){
        windowHeight = res.windowHeight;
      }
    });
    try {
      this.setData({
        windowHeight: windowHeight + 'px'
      })
      this.data.lateQueryArr = wx.getStorageSync('lateQueryArr') || [];
      this.data.id = this.data.fid;
    } catch (e) {

    }

    var that = this;
    that.data.list[0].datas.lateQueryArr = that.data.lateQueryArr;
    that.setData({
      list: that.data.list
    })
    get({
      'path': that.data.url
    }, {
      id: that.data.id
    }).then(function(res) {
      that.setData({
        hidden: true
      });
      var tempData = res;
      if (tempData.ret == 0) {
        var tempFinalData = that.processData(tempData.data)
        that.setData({
          list: tempFinalData
        })
      }
    }, function(res) {
      //关闭loading
      that.setData({
        hidden: true
      });
      //有可能是参数有问题或者是网络
      wx.showToast({
        title: res.data.msg,
        image: require('../../images/com/error.svg')
      })
    })
  },
  attached: function(){
    console.log('attached')
  }
});